home *** CD-ROM | disk | FTP | other *** search
- #ifndef __EVENTLOOP__
- #define __EVENTLOOP__
- #pragma once
-
- #include "Module.h"
- #include "EventLoop.h"
- #include "ToolboxEvent.h"
- #include "EventFilterChain.h"
-
- #include <AppleEvents.h>
- #include <Dialogs.h>
- #include <stdarg.h>
-
-
- class EventLoop;
-
- class EventLoop : public TModule, public EventFilterChain
- {
- DeclareClassSingle(EventLoop, TModule);
-
- public:
- enum AppPhase
- {
- kAppInitializing,
- kAppInitialized,
- kAppActive, // We've just recieved a non-null event
- kAppSleepy, // Received our first null event -- calc sleep time
- kAppSleeping, // We're idle
- kAppDone
- };
-
- private:
- //
- // Toolbox entry points - access with GetXXXProc()
- //
- static pascal Boolean xAEIdleProc(EventRecord *theEvent, long *sleepTime, RgnHandle *mouseRgn);
- static pascal Boolean xAEFilterProc(EventRecord *theEvent, long returnID, long transactionID, const AEAddressDesc *sender);
- static pascal Boolean xModalFilterProc(DialogPtr theDialog, EventRecord *theEvent, DialogItemIndex *itemHit);
- static pascal OSErr xAEEventHandlerProc(const AppleEvent *theAppleEvent, AppleEvent *reply, UInt32 handlerRefcon);
- static pascal OSErr xAEEventHandlerProc1(const AppleEvent *theAppleEvent, AppleEvent *reply, UInt32 handlerRefcon);
-
- //
- // va_arg equivalents of above
- //
- static OSStatus vAEIdleProc(va_list args);
- static OSStatus vAEFilterProc(va_list args);
- static OSStatus vModalFilterProc(va_list args);
- static OSStatus vAEEventHandlerProc(va_list args);
-
- #if GENERATINGCFM
- enum ProcSelector
- {
- kAEIdleProc,
- kAEFilterProc,
- kAEModalFilterProc,
- kAEEventHandlerProc,
- kAEEventHandlerProc1,
- kNumProcSelectors
- };
-
- static RoutineDescriptor gProcDescriptors[kNumProcSelectors];
-
- public:
- static inline AEIdleUPP GetAEIdleProc() { return &gProcDescriptors[kAEIdleProc]; }
- static inline AEFilterUPP GetAEFilterProc() { return &gProcDescriptors[kAEFilterProc]; }
- static inline ModalFilterUPP GetModalFilterProc() { return &gProcDescriptors[kAEModalFilterProc]; }
- static inline AEEventHandlerUPP GetAEEventHandlerProc() { return &gProcDescriptors[kAEEventHandlerProc]; }
- static inline AEEventHandlerUPP GetAEEventHandlerProc1() { return &gProcDescriptors[kAEEventHandlerProc1]; }
- #else
-
- public:
- static inline AEIdleUPP GetAEIdleProc() { return &xAEIdleProc; }
- static inline AEFilterUPP GetAEFilterProc() { return &xAEFilterProc; }
- static inline ModalFilterUPP GetModalFilterProc() { return &xModalFilterProc; }
- static inline AEEventHandlerUPP GetAEEventHandlerProc() { return &xAEEventHandlerProc; }
- static inline AEEventHandlerUPP GetAEEventHandlerProc1() { return &xAEEventHandlerProc1; }
-
- #endif
- protected:
- static EventLoop* gEventLoop;
-
- public:
- static bool ProcessEvent(EventRecord& theEvent, long *sleepTime, RgnHandle *mouseRgn);
- static void Idle();
- static void Yield();
-
- static void* InstallAEHandler(
- AEEventClass theAEEventClass,
- AEEventID theAEEventID,
- AEEventHandlerProcPtr handler,
- Size dataSize = 0);
-
- protected:
- UInt16 fNestLevel; // number of times TBEventProc has been entered
- EventMask fEventMask; // mask for WNE
- UInt32 fSleepTime; // sleep value for WNE
- UInt32 fDreamTime; //
- RgnHandle fMouseRgn; // mouse region for WNE
- AppPhase fAppPhase;
- ToolboxEvent fLastEvent;
- ToolboxEvent fLastMouseUp;
- EventFilterChain fFilterChain;
-
- public:
- EventLoop(EventMask mask = everyEvent);
- virtual ~EventLoop();
-
- virtual void Initialize(void); // Do your thing
- virtual void Finalize(void);
-
- static void Run();
- static void Quit();
-
- static void PushEventFilter(EventFilter& filter);
-
-
- inline UInt32 GetSleepTime() { return fSleepTime; }
- inline RgnHandle GetMouseRgn() { return fMouseRgn; }
-
- protected:
- //
- // Primative entry points --
- //
- virtual bool HandleEventProc(EventRecord& theEvent, long *sleepTime, RgnHandle *mouseRgn);
- virtual bool AEIdleProc(EventRecord& theEvent, long& sleepTime, RgnHandle& mouseRgn);
- virtual bool AEFilterProc(EventRecord& theEvent, long returnID, long transactionID, const AEAddressDesc& sender);
- virtual bool ModalFilterProc(DialogPtr theDialog, EventRecord& theEvent, DialogItemIndex& itemHit);
-
- static void InteractWithUser(long timeOutInTicks = kAEDefaultTimeout,
- NMRecPtr nmReqPtr = nil);
-
- virtual bool PollForEvent(EventRecord& theEvent);
- virtual void DoRun();
- virtual void DoQuit();
- virtual bool DoIdle();
-
- virtual void BeginEvent(ToolboxEvent& theEvent);
- virtual bool FilterEvent(ToolboxEvent& theEvent);
- virtual bool HandleEvent(ToolboxEvent& theEvent);
- virtual bool HandleNullEvent(ToolboxEvent& theEvent);
- virtual bool HandleHighLevelEvent(ToolboxEvent& theEvent);
- virtual void EndEvent(ToolboxEvent& theEvent);
-
- virtual void BeforeEventLoop();
- virtual void AfterEventLoop();
-
- friend class WithNewEvent : public ToolboxEvent
- {
- protected:
- EventLoop& fLoop;
- long* fSleepTime;
- RgnHandle* fMouseRgn;
-
- public:
- WithNewEvent(EventLoop& loop, EventRecord& theEvent, long *sleepTime = nil, RgnHandle *mouseRgn = nil);
- ~WithNewEvent();
- };
-
- };
-
- extern EventLoop& gEventLoop;
-
- inline EventLoop::EventLoop
- (EventMask mask)
- : fNestLevel(0),
- fEventMask(mask),
- fSleepTime(0),
- fDreamTime(6),
- fMouseRgn(nil),
- fAppPhase(kAppInitializing)
- {
- gEventLoop = this;
- }
-
- class FGEventLoop : public EventLoop
- {
- public:
- inline FGEventLoop(EventMask mask)
- : EventLoop(mask) {}
- virtual ~FGEventLoop();
-
- virtual bool HandleEvent(ToolboxEvent& theEvent);
-
- protected:
- virtual bool HandleMouseEvent(ToolboxEvent& theEvent);
- virtual bool HandleKeyboardEvent(ToolboxEvent& theEvent);
- virtual bool HandleWindowEvent(ToolboxEvent& theEvent);
-
- virtual bool HandleMenuSelection(long what);
-
- private:
- typedef EventLoop Inherited;
- };
-
- #endif __EVENTLOOP__
-